<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

class Classs extends Admin
{
	public function index(){
        
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        $name = $this->request->controller() . $this->request->action();
        if($this->request->isPost())
        {
            $post = input('post.');
            $where = [];
            //分组名称
            $where['name'] = $post['name']??'';
            $wheres =[];
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                session($name,$wheres);
                $list = model($this->request->controller())->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where($wheres)->select()->toArray());
            }else{
                Session::delete($name);
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }else{
            if(session($name)){
                $list = model($this->request->controller())->where(session($name))->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->where(session($name))->select()->toArray());
            }else{
                $list = model($this->request->controller())->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model($this->request->controller())->select()->toArray());
            }
        }
        //分类
        $class = model('classs')->select()->toArray();
        $this->assign('class',$class);
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        return $this->fetch();
    }
    //新增
    public function add()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if($this->request->controller() == 'Product'){
                if(strstr($post['images'],'\\')){
                    $post['images'] = str_replace("\\","/",$post['images']);
                }
            }
            model($this->request->controller())->save($post);
            $id = model($this->request->controller())->id;
            if($this->request->controller() == 'Product'){
                if($post['dt']){
                    $dt = explode('%',$post['dt']);
                    array_pop($dt);
                    foreach ($dt as $v){
                        if(strstr($v,'\\')){
                            $v = str_replace("\\","/",$v);
                        }
                        $list[] = ['image'=>$v,'product_id'=>$id];
                    }
                    model('atlas')->saveAll($list);
                }
                $this->redirect('index');
            }
            if($this->request->controller() == 'Classs'){
                return $this->redirect('index/index',['url'=>'classs_index']);
            }else{
            	return 55;
            }
        }
        $list = model('classs')->select()->toArray();
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function view()
    {
        $get = input('get.');
        if(isset($get['battery_order'])){
            $id = $get['battery_order'];
            $list = model($this->request->controller())->where('id','=',$id)->find()->toArray();
            $this->assign('list',$list);
        }
        if($this->request->isPost())
        {
            $post = input('post.');
            pr($post);
            $model = model('classs')->save($post,['id'=>$id]);
            if($model){
                $this->redirect('index');
            }
        }
        return $this->fetch();
    }
    
}
